const jwt = require('jsonwebtoken');
const secretKey = 'itheima No1 ^_^';
const favoriteModel = require('../model/favoriteModel');

module.exports = class favoriteDao {
    // 获取指定用户收藏信息
    static async findFavoriteDao(req, res) {
        try {
            const list = await favoriteModel.getFavorites(req, res);
            if (list.length > 0) {
                res.send({ status: 1, message: 'ok', successMessage: list });
            } else {
                res.send({ status: 0, message: 'error', errorMessage: "该用户暂时没有收藏信息" });
            }
        } catch (error) {
            console.error('Error in findFavoriteDao:', error);
            res.status(500).send({ status: 0, message: 'error', errorMessage: '服务器内部错误' });
        }
    }

    // 添加收藏
    static async addFavoriteDao(req, res) {
        try {
            await favoriteModel.addFavorite(req, res)
                .then((r) => {
                    res.send({ status: 1, message: 'ok', successMessage: r });
                })
                .catch((e) => {
                    res.send({ status: 0, message: 'error', errorMessage: e });
                });
        } catch (error) {
            console.error('Error in addFavoriteDao:', error);
            res.status(500).send({ status: 0, message: 'error', errorMessage: '服务器内部错误' });
        }
    }

    // 删除收藏
    static async deleteFavoriteDao(req, res) {
    try {
        const result = await favoriteModel.deleteFavorite(req, res);
        res.send({ status: 1, message: 'ok', successMessage: result.message });
    } catch (error) {
        console.error('Error in deleteFavoriteDao:', error);
        res.status(500).send({ status: 0, message: 'error', errorMessage: error.message });
    }
}
}